package com.example.isoftservice.service.impl;

import com.example.isoftservice.mapper.CategoryMapper;
import com.example.isoftservice.pojos.Category;
import com.example.isoftservice.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;
    @Override
    @Cacheable(cacheNames ="category",key="#pageNo+pageSize")
    public Map<String, Object> searchCategoryList(Integer pageNo,Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNo,pageSize);
        List<Map<String,Object>> resultList = categoryMapper.selectCategoryList();
        PageInfo<Map<String,Object>> info = new PageInfo(resultList);
        map.put("total",info.getTotal());
        map.put("rows",info.getList());
        return map;
    }

    @Override
    @Cacheable(cacheNames ="category",key="#categoryId")
    public Category searchCategoryById(Integer categoryId) {
        return categoryMapper.selectCategoryById(categoryId);
    }


    @Override
    @CacheEvict(cacheNames = "category" ,allEntries = true)
    public Boolean addCategory(Category category) {
        return categoryMapper.insertCategory(category) == 1 ? true : false;
    }

    @Override
    @CacheEvict(cacheNames = "category" ,allEntries = true)
    public Boolean removeCategoryByIds(Integer[] ids) {
        return categoryMapper.deleteCategoryByIds(ids) >= 1 ? true : false;
    }

    @Override
    @CacheEvict(cacheNames = "category" ,allEntries = true)
    public Boolean modifyCategoryById(Category category) {
        return categoryMapper.updateCategoryById(category) == 1 ? true : false;
    }
}
